<?php

//draw the map with player location co-ordinates
function drawMap($player) {
global $nav;

//get the player's location co-odinates
$playerX = $player->loc_x;
$playerY = $player->loc_y;

//create the default map array
$mapArray = createBaseMapArray($playerX,$playerY); ;

//update the map with data from database
$mapArray = updateMapArray($mapArray, $playerX, $playerY);

$locationDesc =  $mapArray[$playerX][$playerY]['name'];

//now draw the actual map for display
$mymap = '<table class="MapLayer1" border="0" cellpadding="0px" cellspacing="0px"> ';
$mymap .= '<thead> <tr> <th colspan="5">';
$mymap .= 'Map Location X: '.$playerX.', Y: '.$playerY.' - '.$locationDesc;
$mymap .= '</th> </tr> </thead> ';
$mymap .= ' <tfoot> <tr> <td colspan="5" class="MapCentre">';
if ($nav <> '') {
    $mymap .= 'Hero traveled '.$nav.'.';
} else {
    $mymap .= 'Hero idle..';
}
$mymap .= '</td> </tr> </tfoot>';

$x = $playerX - MAP_VIEW_SIZE;
$y = $playerY - MAP_VIEW_SIZE;


$cellCounter = 1;

for ($x=$playerX-MAP_VIEW_SIZE; $x<=$playerX+MAP_VIEW_SIZE; $x++) {
    $mymap .= "<tr>";
    for ($y=$playerY-MAP_VIEW_SIZE; $y<=$playerY+MAP_VIEW_SIZE; $y++) {
        $mapCell = $mapArray[$x][$y];
        $mymap .= '<td width="50px" height="50px"';
        
        //add the navigation links
        switch ($cellCounter) {
            case 8:
                if ($playerX > MAP_X_LOW) {
                    $mymap .= ' class="MapCentre" ><a href="'.FACEBOOK_URL.'?nav=North" >';
                    $mymap .= '<img src="'.IMAGE_HOST_URL.'images/map/'.$mapCell['image'].'" alt="'.$mapCell['name'].'" width="50px" height="50px" /></a><br/>';
                } else {
                    $mymap .= ' ><img src="'.IMAGE_HOST_URL.'images/map/'.$mapCell['image'].'" alt="'.$mapCell['name'].' - Limit of map" width="50px" height="50px" />';
                }
                break;
            case 12:
                if ($playerY > MAP_Y_LOW) {
                    $mymap .= ' class="MapCentre" ><a href="'.FACEBOOK_URL.'?nav=West" >';
                    $mymap .= '<img src="'.IMAGE_HOST_URL.'images/map/'.$mapCell['image'].'" alt="'.$mapCell['name'].'" width="50px" height="50px" /></a><br/>';
                } else {
                    $mymap .= ' ><img src="'.IMAGE_HOST_URL.'images/map/'.$mapCell['image'].'" alt="'.$mapCell['name'].' - Limit of map" width="50px" height="50px" />';
                }
                break;
            case 13:
                $mymap .= ' class="MapCentre" ><a href="'.FACEBOOK_URL.'?action=Search" >';
                $mymap .= '<img src="'.IMAGE_HOST_URL.'images/map/'.$mapCell['image'].'" alt="'.$mapCell['name'].'" width="50px" height="50px" /></a><br/>';
                break;
            case 14:
                if ($playerY < MAP_Y_HIGH) {
                    $mymap .= ' class="MapCentre" ><a href="'.FACEBOOK_URL.'?nav=East" >';
                    $mymap .= '<img src="'.IMAGE_HOST_URL.'images/map/'.$mapCell['image'].'" alt="'.$mapCell['name'].'" width="50px" height="50px" /></a><br/>';
                } else {
                    $mymap .= ' ><img src="'.IMAGE_HOST_URL.'images/map/'.$mapCell['image'].'" alt="'.$mapCell['name'].' - Limit of map" width="50px" height="50px" />';
                }
                break;
            case 18:
                if ($playerX < MAP_X_HIGH) {
                    $mymap .= ' class="MapCentre" ><a href="'.FACEBOOK_URL.'?nav=South" >';
                    $mymap .= '<img src="'.IMAGE_HOST_URL.'images/map/'.$mapCell['image'].'" alt="'.$mapCell['name'].'" width="50px" height="50px" /></a><br/>';
                } else {
                    $mymap .= ' ><img src="'.IMAGE_HOST_URL.'images/map/'.$mapCell['image'].'" alt="'.$mapCell['name'].' - Limit of map" width="50px" height="50px" />';
                }
                break;
            default:
                $mymap .= ' ><img src="'.IMAGE_HOST_URL.'images/map/'.$mapCell['image'].'" alt="'.$mapCell['name'].'" width="50px" height="50px" />';
                
        }
        $cellCounter++;
        $mymap .= "</td>";
    }
    $mymap .= "</tr>";
}


$mymap .= "</table>";

return $mymap;

}


//create the base map array with default land
function createBaseMapArray($playerX, $playerY) {

$solidLand = array('type'=>1,'image'=>'1.jpg','name'=>'Grassland');
    
$mapArray = array(

        $playerX-3 => array(
            $playerY-3 => $solidLand, $playerY-2 => $solidLand, $playerY-1 => $solidLand, $playerY => $solidLand, $playerY+1 => $solidLand, $playerY+3 => $solidLand, $playerY+2 => $solidLand),
        $playerX-2 => array(
            $playerY-3 => $solidLand, $playerY-2 => $solidLand, $playerY-1 => $solidLand, $playerY => $solidLand, $playerY+1 => $solidLand, $playerY+3 => $solidLand, $playerY+2 => $solidLand),
        $playerX-1 => array(
            $playerY-3 => $solidLand, $playerY-2 => $solidLand, $playerY-1 => $solidLand, $playerY => $solidLand, $playerY+1 => $solidLand, $playerY+3 => $solidLand, $playerY+2 => $solidLand),
        $playerX => array(
            $playerY-3 => $solidLand, $playerY-2 => $solidLand, $playerY-1 => $solidLand, $playerY => $solidLand, $playerY+1 => $solidLand, $playerY+3 => $solidLand, $playerY+2 => $solidLand),
        $playerX+1 => array(
            $playerY-3 => $solidLand, $playerY-2 => $solidLand, $playerY-1 => $solidLand, $playerY => $solidLand, $playerY+1 => $solidLand, $playerY+3 => $solidLand, $playerY+2 => $solidLand),
        $playerX+2 => array(
            $playerY-3 => $solidLand, $playerY-2 => $solidLand, $playerY-1 => $solidLand, $playerY => $solidLand, $playerY+1 => $solidLand, $playerY+3 => $solidLand, $playerY+2 => $solidLand),
        $playerX+3 => array(
            $playerY-3 => $solidLand, $playerY-2 => $solidLand, $playerY-1 => $solidLand, $playerY => $solidLand, $playerY+1 => $solidLand, $playerY+3 => $solidLand, $playerY+2 => $solidLand)
        );

return $mapArray;
    
}

function updateMapArray($mapArray, $playerX, $playerY){

    //calc SQL data values
    $playerXlow = $playerX - MAP_VIEW_SIZE;
    $playerXhigh = $playerX + MAP_VIEW_SIZE;

    $playerYlow = $playerY - MAP_VIEW_SIZE;
    $playerYhigh = $playerY + MAP_VIEW_SIZE;

    $query = "SELECT * FROM `map` WHERE (`x` BETWEEN $playerXlow AND $playerXhigh) AND (`y` BETWEEN $playerYlow AND $playerYhigh) ORDER BY `x`, `y` ";

    $result = mysql_query($query);
    //replace map array default with data from database
    if ($result) {
        while ($row = mysql_fetch_object($result)) {
            $mapX = $row->x;
            $mapY = $row->y;
        //    echo "mapX: $mapX, mapY: $mapY ";
            $mapArray[$mapX][$mapY]['type'] = $row->type;
            $mapArray[$mapX][$mapY]['image'] = $row->image;
            $mapArray[$mapX][$mapY]['name'] = $row->name;
        //    echo "row[image] $row[image] ";
        }
    }

return $mapArray;

}



  
?>
